﻿#ifndef SCALESLIDER_H
#define SCALESLIDER_H

#include <QWidget>

class ScaleSlider : public QWidget
{
    Q_PROPERTY(double m_dCurValue READ value WRITE setValue NOTIFY sigValueChanged);

    enum SliderType
    {
        NoSlider,       //无滑块
        Rectangle,      //矩形
        Triangle,       //三角形
        Circular,       //圆形
    };

    enum CurValueDisplayMode
    {
        NoCurValue,     //无当前值
        OnAddPageFront, //在添加页前端
        OnAddPageMiddle,//在添加页中间
        OnAddPageBack,  //在添加页末尾
        OnGrooveLeft,   //在沟槽左边
        OnGrooveRight,  //在沟槽右边
    };

    Q_OBJECT
public:
    explicit ScaleSlider(QWidget *parent = nullptr);

    void initialize();
    void setRadius(double xRadiusScaleFactor,double yRadiusScaleFactor);

    //沟槽
    void setGrooveVisible(bool state);
    void setGrooveScaleFactor(double widthScaleFactor, double heightScaleFactor);
    void setGrooveColor(QColor color);
    void setGrooveGradientColor(QPair<QColor,QColor> gradientColor);

    //刻度
    double maximum() const;
    double minimum() const;
    void setRange(double min, double max);
    void setScaleVisible(bool state);
    void setScaleLineColor(QColor color);
    void setScaleValueColor(QColor color);
    void setLargeLineScaleFactor(double widthScaleFactor, double heightScaleFactor);
    void setSmallLineScaleFactor(double widthScaleFactor, double heightScaleFactor);
    void setLargeScaleSegments(int segments);
    void setSmallScaleSegments(int segments);
    void setScaleValueFont(QFont font);

    //滑块
    void setSliderVisible(bool state);
    void setSliderScaleFactor(double widthScaleFactor, double heightScaleFactor);
    void setSliderColor(QColor color);
    void setSliderType(SliderType type);
    void setSliderGradient(QGradient gradient);
    void setSliderGradientColor(QPair<QColor,QColor> gradientColor);

    //添加页
    void setAddPageVisible(bool state);
    void setAddPageScaleFactor(double widthScaleFactor, double heightScaleFactor);
    void setAddPageColor(QColor color);
    void setAddPageGradientColor(QPair<QColor,QColor> gradientColor);

    //当前值
    double value() const;
    void setValueFont(QFont font);
    void setValueVisible(bool state);
    void setValueDisplayMode(CurValueDisplayMode mode);
    void setValueSlidable(bool state);
    void setValue(double value);
    void add(double value);
    void setFont(QFont font);
    void setValueDecimal(int decimal);
    void setValueColor(QColor color);
    void setValueUnit(QString unit);

protected:
    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent*event) override;
    void resizeEvent(QResizeEvent *event) override;

private:
    void calcParameters();

    void drawGroove(QPainter* painter);
    void drawScale(QPainter* painter);
    void drawAddPage(QPainter* painter);
    void drawCurValue(QPainter* painter);
    void drawSlider(QPainter* painter);

signals:
    void sigValueChanged(double value);


private:

    bool is_class_initialized = false;  //是否初始化完成
    bool m_bIsSlidable = true;          //是否能够滑动改变值
    double m_dMinimum = 0;              //刻度最小值
    double m_dMaximum = 100;              //刻度最大值
    double m_dXRadiusScaleFactor = 2.5;   //矩形圆角X轴半径比例因子(0~100%,矩形width)
    double m_dYRadiusScaleFactor = 100;   //矩形圆角Y轴半径比例因子(0~100%,矩形height)
    double m_dXRadius = 0;              //矩形圆角X轴半径
    double m_dYRadius = 0;              //矩形圆角Y轴半径

    //沟槽
    bool m_bGrooveVisible = true;           //沟槽是否可见
    double m_dGrooveWidthScaleFactor = 100;   //沟槽宽度比例因子(0~100%,this->width)
    double m_dGrooveHeightScaleFactor = 100;  //沟槽高度比例因子(0~100%,this->Height)
    double m_dGrooveWidth = 0;              //沟槽宽度
    double m_dGrooveHeight = 0;             //沟槽高度
    QColor m_grooveColor = QColor(147, 157, 174);        //沟槽画笔颜色(边框颜色)
    QPair<QColor,QColor> m_grooveGradientColor;  //沟槽填充渐变颜色(起始，结束)
    QGradient m_grooveGradient = QLinearGradient(0,0,0,0); //沟槽填充渐变颜色梯度

    //刻度
    bool m_bLargeScaleVisible = true;               //大刻度线是否可见
    QPoint m_scaleStartingPoints = QPoint(0,0);     //刻度线起始坐标
    QColor m_largeScaleLineColor = QColor(150,160,176);   //大刻度颜色
    int m_iLargeScaleSegments = 5;                  //大刻度段数
    double m_dLargeScaleLineWidthScaleFactor = 2;   //大刻度线宽度比例因子(0~100%,GrooveWidth/m_iSegments)
    double m_dLargeScaleLineLengthScaleFactor = 30;  //大刻度线长度比例因子(0~100%,GrooveHeight)
    double m_dLargeScaleLineWidth = 0;   //大刻度线宽度
    double m_dLargeScaleLineLength = 0;  //大刻度线长度

    bool m_bSmallScaleVisible = true;               //小刻度线是否可见
    int m_iSmallScaleSegments = 5;                  //小刻度段数
    QColor m_smallScaleLineColor = QColor(150,160,176);   //小刻度颜色
    double m_dSmallScaleLineWidthScaleFactor = 2;   //小刻度线宽度比例因子(0~100%,GrooveWidth/m_iSegments)
    double m_dSmallScaleLineLengthScaleFactor = 15;  //小刻度线长度比例因子(0~100%,GrooveHeight)
    double m_dSmallScaleLineWidth = 0;   //小刻度线宽度
    double m_dSmallScaleLineLength = 0;  //小刻度线长度

    int m_iSegments = 0;                            //总段数

    bool m_bScaleValueVisible = true;               //刻度值是否可见
    QColor m_scaleValueColor = QColor(150,160,176);            //刻度值画笔颜色(边框颜色)
    QFont m_scaleValueFont = QFont("宋体", 8);               //刻度值字体

    //滑块
    bool m_bSliderVisible = true;                   //滑块是否可见
    double m_dSliderWidthScaleFactor = 10;           //滑块宽度比例因子(0~100%,this->width)
    double m_dSliderHeightScaleFactor = 100;          //滑块高度比例因子(0~100%,this->Height)
    double m_dSliderWidth = 0;                      //滑块宽度
    double m_dSliderHeight = 0;                     //滑块高度
    QColor m_sliderColor = QColor(147, 157, 174);                //滑块画笔颜色(边框颜色)
    SliderType m_sliderType = ScaleSlider::Rectangle;   //滑块类型
    QPair<QColor,QColor> m_sliderGradientColor;  //滑块填充渐变颜色(起始，结束)
    QGradient m_sliderGradient = QLinearGradient(0,0,0,0);  //滑块填充渐变颜色梯度

    //添加页
    bool m_bAddPageVisible = true;          //添加页是否可见
    double m_dAddPageWidthScaleFactor = 100;  //添加页宽度比例因子(0~100%,this->width)
    double m_dAddPageHeightScaleFactor = 100; //添加页高度比例因子(0~100%,this->Height)
    double m_dAddPageWidth = 0;             //添加页宽度
    double m_dAddPageHeight = 0;            //添加页高度
    double m_dAddPageCurWidth = 0;          //添加页当前宽度
    QColor m_addPageColor = QColor(147, 157, 174);       //添加页画笔颜色(边框颜色)
    QPair<QColor,QColor> m_addPageGradientColor;  //添加页填充渐变颜色(起始，结束)
    QGradient m_addPageGradient = QLinearGradient(0,0,0,0); //添加页填充渐变颜色梯度

    //当前值
    bool m_bCurValueVisible = true;     //当前值是否可见
    double m_dCurValue = 0;                 //当前值
    int m_iCurValueDecimal = 0;         //保留当前值小数位置
    QColor m_curValueColor = QColor(255,255,255);  //当前值画笔颜色(边框颜色)
    QFont m_curValueFont = QFont("宋体", 20, QFont::Bold);     //当前值字体
    QString m_szCurValueUnit = "";      //当前值单位
    CurValueDisplayMode m_curValueDisplayMode
                        = ScaleSlider::OnAddPageMiddle; //当前值显示模式
};

#endif // SCALESLIDER_H
